package world;

public class StopWatch {

	private long elapsedTime;
	private boolean isRunning;
	private boolean isPaused;
	private long timeStart;
	private long timeComplete;
	private long pauseTime;
	private long resumeTime;

	public StopWatch() {

		isRunning = false;
		isPaused = false;
		elapsedTime = 0;

	}

	public void start() {

		if (!isRunning && !isPaused) {
			elapsedTime = 0;
			timeStart = System.currentTimeMillis();
			isRunning = true;

		}

	}

	public void stop() {

		if (isRunning && !isPaused) {

			timeComplete = System.currentTimeMillis();
			isRunning = false;

		}
	}

	public void pause() {

		if (isRunning && !isPaused) {

			pauseTime = System.currentTimeMillis();
			isPaused = true;

		}

	}

	public void resume() {

		if (isRunning && isPaused) {

			resumeTime = System.currentTimeMillis();
			isPaused = false;
			timeStart += (resumeTime - pauseTime);
		}

	}

	public long getElapsedTime() {

		if (isRunning && !isPaused) {

			elapsedTime = ((System.currentTimeMillis() - timeStart) / 1000);

		} else {
			if (!isRunning && !isPaused) {
				elapsedTime = ((timeComplete - timeStart) / 1000);
			} else {

				elapsedTime = ((pauseTime - timeStart) / 1000);

			}
		}

		return elapsedTime;

	}

}
